package com.yoo.module_base.view.index.slideview

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView
import com.yoo.module_base.view.index.slideview.hp.IndexSlideCardDataObserver
import com.yoo.module_base.view.index.slideview.hp.IndexSlideCardSnapHelper

class IndexSlideCardView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0
) : RecyclerView(context, attrs, defStyle) {
    private val observer: IndexSlideCardDataObserver = IndexSlideCardDataObserver(this)

    init {
        initialize()
    }

    override fun setLayoutManager(manager: RecyclerView.LayoutManager?) {
        if (manager is IndexSlideCardLayoutManager) {
            super.setLayoutManager(manager)
        } else {
            throw IllegalArgumentException("CardStackView must be set CardStackLayoutManager.")
        }
    }

    override fun setAdapter(adapter: Adapter<*>?) {
        if (layoutManager == null) {
            layoutManager = IndexSlideCardLayoutManager(
                context
            )
        }
        // Imitate RecyclerView's implementation
        // http://tools.oesf.biz/android-9.0.0_r1.0/xref/frameworks/base/core/java/com/android/internal/widget/RecyclerView.java#1005
        if (getAdapter() != null) {
            getAdapter()?.unregisterAdapterDataObserver(observer)
            getAdapter()?.onDetachedFromRecyclerView(this)
        }
        adapter?.registerAdapterDataObserver(observer)
        super.setAdapter(adapter)
    }

    override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        if (event.action == MotionEvent.ACTION_DOWN) {
            val manager = layoutManager as IndexSlideCardLayoutManager?
            manager?.updateProportion(event.x, event.y)
        }
        return super.onInterceptTouchEvent(event)
    }

    fun swipe() {
        if (layoutManager is IndexSlideCardLayoutManager) {
            val manager = layoutManager as IndexSlideCardLayoutManager
            smoothScrollToPosition(manager.topPosition + 1)
        }
    }

    fun rewind() {
        if (layoutManager is IndexSlideCardLayoutManager) {
            val manager = layoutManager as IndexSlideCardLayoutManager
            smoothScrollToPosition(manager.topPosition - 1)
        }
    }

    private fun initialize() {
        IndexSlideCardSnapHelper().attachToRecyclerView(this)
        overScrollMode = RecyclerView.OVER_SCROLL_NEVER
    }
}
